<template>
  <el-card class="box-card">
    <!-- 搜索框和新增按钮 -->
    <el-row :gutter="20" style="margin-bottom: 20px;">
      <el-col :span="6">
        <el-input
          v-model="searchKeyword"
          placeholder="请输入姓名、站点或邮箱"
          size="mini"
          clearable
          @keyup.enter="handleSearch"
        >
          <template #append>
            <el-button icon="el-icon-search" @click="handleSearch" />
          </template>
        </el-input>
      </el-col>
      <el-col :span="1">
        <el-button size="mini" icon="el-icon-circle-plus-outline" type="primary" @click="add">
          新增
        </el-button>
      </el-col>
    </el-row>

    <!-- Dialog 对话框 弹出新增和修改表单 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="id:" hidden>
          <el-input v-model="form.id"></el-input>
        </el-form-item>
        <el-form-item label="姓名:" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" style="width:80%"></el-input>
        </el-form-item>
        <el-form-item label="站点:" prop="age">
          <el-input v-model="form.age" placeholder="请输入站点" style="width:80%"></el-input>
        </el-form-item>
        <el-form-item label="邮箱:" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" style="width:80%"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit()">提 交</el-button>
      </div>
    </el-dialog>

    <!-- 表格 -->
    <el-table ref="singleTable" :data="filteredTableData" style="width: 100%">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column property="name" label="负责人姓名" width="120" align="center"></el-table-column>
      <el-table-column property="age" label="站点" width="120" align="center"></el-table-column>
      <el-table-column property="email" label="邮箱" width="120" align="center"></el-table-column>
      <el-table-column label="操作" min-width="180" align="center">
        <template slot-scope="scope">
          <div style="display: flex; justify-content: center;">
            <el-button size="mini" icon="el-icon-edit" @click="edit(scope.$index, scope.row)">
              编辑
            </el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="remove(scope.$index, scope.row)">
              删除
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      style="margin-top: 20px; text-align: center;"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </el-card>
</template>

<script>
export default {
  name: "student",
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 0,
      title: '',
      dialogFormVisible: false,
      form: {},
      tableData: [],
      searchKeyword: '', // 搜索关键词
      rules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        age: [{ required: true, message: '请输入站点', trigger: 'blur' }],
        email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }]
      }
    };
  },
  computed: {
    // 过滤后的表格数据
    filteredTableData() {
      if (!this.searchKeyword) return this.tableData;
      const keyword = this.searchKeyword.toLowerCase();
      return this.tableData.filter(
        (item) =>
          item.name.toLowerCase().includes(keyword) ||
          item.age.toLowerCase().includes(keyword) ||
          item.email.toLowerCase().includes(keyword)
      );
    }
  },
  methods: {
    // 表单重置初始化
    reset() {
      this.form = {
        id: null,
        name: null,
        age: null,
        email: null
      };
    },

    // 新增
    add() {
      this.reset();
      this.dialogFormVisible = true;
      this.title = "新增数据";
    },

    // 删除
    remove(index, row) {
      this.$axios({
        method: 'post',
        url: 'http://localhost:9090/student/remove/' + row.id,
      }).then((response) => {
        this.$message({
          message: '删除成功!',
          type: 'success'
        });
        this.getList();
      }).catch((error) => {
      });
    },

    // 编辑
    edit(index, row) {
      this.reset();
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogFormVisible = true;
      this.title = "修改数据";
    },

    // 查询
    getList() {
      this.$axios({
        method: 'get',
        url: 'http://localhost:9090/student/info',
      }).then((response) => {
        this.tableData = response.data;
        this.total = response.data.length; // 更新总条数
      }).catch((error) => {
      });
    },

    // 提交
    submit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const url = this.form.id ? 'http://localhost:9090/student/edit' : 'http://localhost:9090/student/add';
          this.$axios({
            method: 'post',
            data: this.form,
            url: url,
          }).then((response) => {
            this.$message({
              message: this.form.id ? '修改成功!' : '新增成功!',
              type: 'success'
            });
            this.dialogFormVisible = false;
            this.getList();
          }).catch((error) => {
          });
        } else {
          return false;
        }
      });
    },

    // 搜索
    handleSearch() {
      // 搜索逻辑已在 computed 中实现
    },

    // 分页大小改变
    handleSizeChange(val) {
      this.pageSize = val;
      this.getList();
    },

    // 当前页改变
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    }
  },
  mounted() {
    this.getList();
  }
};
</script>

<style scoped>
.box-card {
  margin: 20px;
}
</style>
